package com.x.schedule.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.lang.reflect.Method;

/**
 * ClassName: BaseController
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.08.26 16:08
 */
public class BaseController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String[] split = requestURI.split("/");//以“/”分割成多个部分
        String methodName = split[split.length - 1];

        /*if("add".equals(methodName)){
            //增加处理
            add(req,resp);
        }else if("remove".equals(methodName)){
            remove(req,resp);
        }else if("update".equals(methodName)){
            update(req,resp);
        }else if("find".equals(methodName)){
            find(req,resp);
        }*/

        Class aClass = this.getClass();
        try {

            //获取方法
            Method method = aClass.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //破解访问修饰符限制
            method.setAccessible(true);
            //执行方法
            method.invoke(this, req, resp);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
